package light.head.dao.site;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.CommodityShow;

import org.springframework.stereotype.Service;

@Service
public class CommodityShowDAO extends BaseDAO<CommodityShow, Serializable> {
	private static final String sql_indexlist = "FROM CommodityShow cs WHERE cs.deleted = 0 AND cs.oc = ? ORDER BY cs.sort";
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM Commodity cs WHERE 1 = 1");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();
			String name = map.get(Parm.NAME);
			String sort = map.get(Parm.SORT);
			String deleted = map.get(Parm.DELETED);
			String oc = map.get(Parm.OC);
			if (oc != "" && isNotNull(oc)) {
				hql.append(" AND cs.oc = ? ");
				list.add(Integer.parseInt(oc));
			}
			if (name != "" && isNotNull(name)) {
				hql.append(" AND cs.name LIKE ? ");
				list.add("%" + name + "%");
			}
			if (sort != "" && isNotNull(sort)) {
				hql.append(" AND cs.sort = ? ");
				list.add(Integer.parseInt(sort));
			}
			if (deleted != "" && isNotNull(deleted)) {
				hql.append(" AND cs.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY cs.code");

		return findToPageBean(hql.toString(), pageBean, list);
	}

	public List<CommodityShow> findList() {
		return super.find("FROM CommodityShow cs WHERE cs.deleted = 0 ORDER BY cs.sort");
	}
	
	//获取首页List
	public List<CommodityShow> getIndexList(Integer code){
		return super.find(sql_indexlist, code);
	}
}
